package com.ewili.x.special.task.thread.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.x.special.dao.inter.ValueDao;
import com.ewili.x.special.dao.inter.ValueStockDao;
import com.ewili.x.special.entity.Value;
import com.ewili.x.special.entity.ValueStock;
import com.ewili.x.special.task.thread.inter.ValueThreadDaoTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author wonder
 */
@Slf4j
@Component
public class ValueThreadDaoTaskImpl implements ValueThreadDaoTask {

    @Autowired
    private ValueDao valueDao;

    @Autowired
    private ValueStockDao valueStockDao;

    /**
     * 保存数据
     *
     * @param object
     */
    //@Async("myTaskAsyncPool")
    @Override
    public void saveData(JSONObject object, String code) {
        Date date = object.getSqlDate("TRADEDATE");
        Double close = "-".equalsIgnoreCase(object.getString("NEW")) ? 0.0d : object.getDouble("NEW");
        if (close == 0.0d) {
            return;
        }
        Double changeRate = "-".equalsIgnoreCase(object.getString("CHG")) ? 0.0d : object.getDouble("CHG");
        Double totalValue = "-".equalsIgnoreCase(object.getString("ZSZ")) ? 0.0d : object.getDouble("ZSZ");
        if (totalValue == 0.0d) {
            return;
        }
        Double circulationValue = object.getDouble("AGSZBHXS");
        Double peTtm = "-".equalsIgnoreCase(object.getString("PE9")) ? 0.0d : object.getDouble("PE9");
        Double pe = "-".equalsIgnoreCase(object.getString("PE7")) ? 0.0d : object.getDouble("PE7");
        Double pb = "-".equalsIgnoreCase(object.getString("PB8")) ? 0.0d : object.getDouble("PB8");
        Double peg = "-".equalsIgnoreCase(object.getString("PEG1")) ? 0.0d : object.getDouble("PEG1");
        Double pcf = "-".equalsIgnoreCase(object.getString("PCFJYXJL9")) ? 0.0d : object.getDouble("PCFJYXJL9");
        Double ps = "-".equalsIgnoreCase(object.getString("PS9")) ? 0.0d : object.getDouble("PS9");
        String hyCode = object.getString("HYCode");

        Boolean isExists = valueDao.existsByCodeAndDate(code, date);
        if (isExists) {
            return;
        }

        Value value = new Value();
        value.setCode(code);
        value.setDate(date);
        value.setClose(close);
        value.setChangeRate(changeRate);
        value.setTotalValue(totalValue);
        value.setCirculationValue(circulationValue);
        value.setPeTtm(peTtm);
        value.setPe(pe);
        value.setPb(pb);
        value.setPeg(peg);
        value.setPcf(pcf);
        value.setPs(ps);
        value.setHyCode(hyCode);

        valueDao.save(value);
    }

    //@Async
    @Override
    public void saveValueStockData(JSONArray jsonArray) {

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String code = jsonObject.getString("SECUCODE").replace(".SZ", "").replace(".SH", "");
            Date date = jsonObject.getSqlDate("TRADE_DATE");


            Double indicatorType = jsonObject.getDouble("INDICATOR_TYPE");
            Double indexValue = jsonObject.getDouble("INDEX_VALUE");
            Double indexPercentile = jsonObject.getDouble("INDEX_PERCENTILE");
            String indexPercentResult = jsonObject.getString("VALATION_STATUS");


            ValueStock valueStock = valueStockDao.findByCode(code);

            if (valueStock == null) {
                valueStock = new ValueStock();
            }

            valueStock.setCode(code);
            valueStock.setDate(date);

            if (indicatorType.equals(1.0d)) {
                valueStock.setPe(indexValue);
                valueStock.setPePercentAll(indexPercentile);
                valueStock.setPePercentAllResult(indexPercentResult);
            } else if (indicatorType.equals(2.0d)) {

                valueStock.setPb(indexValue);
                valueStock.setPbPercentAll(indexPercentile);
                valueStock.setPbPercentAllResult(indexPercentResult);

            } else if (indicatorType.equals(3.0d)) {

                valueStock.setPs(indexValue);
                valueStock.setPsPercentAll(indexPercentile);
                valueStock.setPsPercentAllResult(indexPercentResult);
            } else if (indicatorType.equals(4.0d)) {

                valueStock.setPcf(indexValue);
                valueStock.setPcfPercentAll(indexPercentile);
                valueStock.setPcfPercentAllResult(indexPercentResult);
            }

            valueStockDao.save(valueStock);

        }

    }

    //@Async
    @Override
    public void saveValuePercentData(JSONArray jsonArray) {

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String code = jsonObject.getString("SECUCODE").replace(".SZ", "").replace(".SH", "");


            Double indicatorType = jsonObject.getDouble("INDEX_TYPE");
            Double statisticsCycle = jsonObject.getDouble("STATISTICS_CYCLE");
            Double percentileThirty = jsonObject.getDouble("PERCENTILE_THIRTY");
            Double percentileFifty = jsonObject.getDouble("PERCENTILE_FIFTY");
            Double percentileSeventy = jsonObject.getDouble("PERCENTILE_SEVENTY");


            ValueStock valueStock = valueStockDao.findByCode(code);

            if (valueStock == null) {
                continue;
            }


            if (indicatorType.equals(1.0d) && statisticsCycle.equals(1.0d)) {
                valueStock.setPeOneYearThirtyPercent(percentileThirty);
                valueStock.setPeOneYearFiftyPercent(percentileFifty);
                valueStock.setPeOneYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(1.0d) && statisticsCycle.equals(2.0d)) {
                valueStock.setPeThreeYearThirtyPercent(percentileThirty);
                valueStock.setPeThreeYearFiftyPercent(percentileFifty);
                valueStock.setPeThreeYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(1.0d) && statisticsCycle.equals(3.0d)) {

                valueStock.setPeFiveYearThirtyPercent(percentileThirty);
                valueStock.setPeFiveYearFiftyPercent(percentileFifty);
                valueStock.setPeFiveYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(1.0d) && statisticsCycle.equals(4.0d)) {

                valueStock.setPeTenYearThirtyPercent(percentileThirty);
                valueStock.setPeTenYearFiftyPercent(percentileFifty);
                valueStock.setPeTenYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(2.0d) && statisticsCycle.equals(1.0d)) {
                valueStock.setPbOneYearThirtyPercent(percentileThirty);
                valueStock.setPbOneYearFiftyPercent(percentileFifty);
                valueStock.setPbOneYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(2.0d) && statisticsCycle.equals(2.0d)) {
                valueStock.setPbThreeYearThirtyPercent(percentileThirty);
                valueStock.setPbThreeYearFiftyPercent(percentileFifty);
                valueStock.setPbThreeYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(2.0d) && statisticsCycle.equals(3.0d)) {
                valueStock.setPbFiveYearThirtyPercent(percentileThirty);
                valueStock.setPbFiveYearFiftyPercent(percentileFifty);
                valueStock.setPbFiveYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(2.0d) && statisticsCycle.equals(4.0d)) {
                valueStock.setPbTenYearThirtyPercent(percentileThirty);
                valueStock.setPbTenYearFiftyPercent(percentileFifty);
                valueStock.setPbTenYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(3.0d) && statisticsCycle.equals(1.0d)) {
                valueStock.setPsOneYearThirtyPercent(percentileThirty);
                valueStock.setPsOneYearFiftyPercent(percentileFifty);
                valueStock.setPsOneYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(3.0d) && statisticsCycle.equals(2.0d)) {
                valueStock.setPsThreeYearThirtyPercent(percentileThirty);
                valueStock.setPsThreeYearFiftyPercent(percentileFifty);
                valueStock.setPsThreeYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(3.0d) && statisticsCycle.equals(3.0d)) {
                valueStock.setPsFiveYearThirtyPercent(percentileThirty);
                valueStock.setPsFiveYearFiftyPercent(percentileFifty);
                valueStock.setPsFiveYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(3.0d) && statisticsCycle.equals(4.0d)) {
                valueStock.setPsTenYearThirtyPercent(percentileThirty);
                valueStock.setPsTenYearFiftyPercent(percentileFifty);
                valueStock.setPsTenYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(4.0d) && statisticsCycle.equals(1.0d)) {
                valueStock.setPcfOneYearThirtyPercent(percentileThirty);
                valueStock.setPcfOneYearFiftyPercent(percentileFifty);
                valueStock.setPcfOneYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(4.0d) && statisticsCycle.equals(2.0d)) {
                valueStock.setPcfThreeYearThirtyPercent(percentileThirty);
                valueStock.setPcfThreeYearFiftyPercent(percentileFifty);
                valueStock.setPcfThreeYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(4.0d) && statisticsCycle.equals(3.0d)) {
                valueStock.setPcfFiveYearThirtyPercent(percentileThirty);
                valueStock.setPcfFiveYearFiftyPercent(percentileFifty);
                valueStock.setPcfFiveYearSeventyPercent(percentileSeventy);
            } else if (indicatorType.equals(4.0d) && statisticsCycle.equals(4.0d)) {
                valueStock.setPcfTenYearThirtyPercent(percentileThirty);
                valueStock.setPcfTenYearFiftyPercent(percentileFifty);
                valueStock.setPcfTenYearSeventyPercent(percentileSeventy);
            }
            valueStockDao.save(valueStock);

        }

    }

    @Override
    public void saveValueStockProbablity(JSONObject jsonObject) {
        Boolean success = jsonObject.getBoolean("success");
        if (!success) {
            return;
        }

        JSONObject object = jsonObject.getJSONObject("result");
        JSONArray array = object.getJSONArray("data");


        Integer indexType = null;

        String code  = null;

        Double riseProbabilityThree = null;
        Double averageIncreaseThree = null;

        Double riseProbabilitySix = null;
        Double averageIncreaseSix = null;

        Double riseProbabilityOneYear = null;
        Double averageIncreaseOneYear = null;

        Double riseProbabilityThreeYears = null;
        Double averageIncreaseThreeYears = null;

        Date tradeDate = new Date();

        for (int i = 0; i < array.size(); i++) {
            JSONObject json = array.getJSONObject(i);
            code = json.getString("SECUCODE");
            code = StringUtils.removeAll(code,".SZ");
            code = StringUtils.removeAll(code,".SH");
            Integer dataType = json.getInteger("DATE_TYPE");
            indexType = json.getInteger("INDEX_TYPE");
            tradeDate = json.getSqlDate("TRADE_DATE");
            if (dataType.equals(1)) {
                riseProbabilityThree = json.getDouble("RISE_PROBABILITY");
                averageIncreaseThree = json.getDouble("AVERAGE_INCREASE");

            }
            if (dataType.equals(2)) {
                riseProbabilitySix = json.getDouble("RISE_PROBABILITY");
                averageIncreaseSix = json.getDouble("AVERAGE_INCREASE");
            }
            if (dataType.equals(3)) {
                riseProbabilityOneYear = json.getDouble("RISE_PROBABILITY");
                averageIncreaseOneYear = json.getDouble("AVERAGE_INCREASE");
            }
            if (dataType.equals(4)) {
                riseProbabilityThreeYears = json.getDouble("RISE_PROBABILITY");
                averageIncreaseThreeYears = json.getDouble("AVERAGE_INCREASE");
            }

            ValueStock valueStock = valueStockDao.findByCode(code);
            if (valueStock == null) {
                continue;
            }


            if (indexType.equals(1)) {
                valueStock.setPeRiseProbabilityThree(riseProbabilityThree);
                valueStock.setPeAverageIncreaseThree(averageIncreaseThree);
                valueStock.setPeRiseProbabilitySix(riseProbabilitySix);
                valueStock.setPeAverageIncreaseSix(averageIncreaseSix);
                valueStock.setPeRiseProbabilityOneYear(riseProbabilityOneYear);
                valueStock.setPeAverageIncreaseOneYear(averageIncreaseOneYear);
                valueStock.setPeRiseProbabilityThreeYear(riseProbabilityThreeYears);
                valueStock.setPeAverageIncreaseThreeYear(averageIncreaseThreeYears);
            } else if (indexType.equals(2)) {

                valueStock.setPbRiseProbabilityThree(riseProbabilityThree);
                valueStock.setPbAverageIncreaseThree(averageIncreaseThree);
                valueStock.setPbRiseProbabilitySix(riseProbabilitySix);
                valueStock.setPbAverageIncreaseSix(averageIncreaseSix);
                valueStock.setPbRiseProbabilityOneYear(riseProbabilityOneYear);
                valueStock.setPbAverageIncreaseOneYear(averageIncreaseOneYear);
                valueStock.setPbRiseProbabilityThreeYear(riseProbabilityThreeYears);
                valueStock.setPbAverageIncreaseThreeYear(averageIncreaseThreeYears);

            } else if (indexType.equals(3)) {

                valueStock.setPsRiseProbabilityThree(riseProbabilityThree);
                valueStock.setPsAverageIncreaseThree(averageIncreaseThree);
                valueStock.setPsRiseProbabilitySix(riseProbabilitySix);
                valueStock.setPsAverageIncreaseSix(averageIncreaseSix);
                valueStock.setPsRiseProbabilityOneYear(riseProbabilityOneYear);
                valueStock.setPsAverageIncreaseOneYear(averageIncreaseOneYear);
                valueStock.setPsRiseProbabilityThreeYear(riseProbabilityThreeYears);
                valueStock.setPsAverageIncreaseThreeYear(averageIncreaseThreeYears);
            } else if (indexType.equals(4)) {

                valueStock.setPcfRiseProbabilityThree(riseProbabilityThree);
                valueStock.setPcfAverageIncreaseThree(averageIncreaseThree);
                valueStock.setPcfRiseProbabilitySix(riseProbabilitySix);
                valueStock.setPcfAverageIncreaseSix(averageIncreaseSix);
                valueStock.setPcfRiseProbabilityOneYear(riseProbabilityOneYear);
                valueStock.setPcfAverageIncreaseOneYear(averageIncreaseOneYear);
                valueStock.setPcfRiseProbabilityThreeYear(riseProbabilityThreeYears);
                valueStock.setPcfAverageIncreaseThreeYear(averageIncreaseThreeYears);
            }

            valueStockDao.save(valueStock);
        }




    }
}
